package com.example.xbrother.rule.battery;

import com.example.xbrother.rule.BaseBean;
import com.example.xbrother.rule.MaterialResult;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 蓄电池类型-bean
 */
@Slf4j
public class BatteryTypeBean extends BaseBean {

    /**
     * 添加物料
     */
    public void add(BatteryTypeParam p, List<List<String>> list) {
        List<MaterialResult> r = p.getResult();
        int batteryNum = 30;
        if (p.getTotalPower() >= 60) {
            batteryNum = 40;
        }
        // W= 160000/（40*6*0.95）=701.75  (Watts/cell)
        double powerEach = p.getTotalPower() * 1000 / (batteryNum * 6 * 0.95);
        // 每节蓄电池的最小功率
        log.info("powerEach = {}", powerEach);
        // 比对，取最优解
        // list = [[38ah, 62.6], [65ah, 107], [100ah, 164.7], [150ah, 247], [200ah, 329.3], [250ah, 411.7]]
        log.info("list = {}", list);
        int len = list.size();
        // 把蓄电池的功率组成一个 数组，便于后续判断
        double[] powerList = new double[len];
        for (int i = 0; i < list.size(); i++) {
            List<String> item = list.get(i);
            String powerStr = item.get(1);
            double powerItem = Double.parseDouble(powerStr);
            powerList[i] = powerItem;
        }
        log.info("powerList = {}", powerList);
        // 查找最接近的倍数值
        int[] rTemp = latestData(powerList, powerEach, 2);
        if (rTemp != null) {
            int index = rTemp[0];
            int num = rTemp[1];
            MaterialResult materialResult = new MaterialResult(list.get(index).get(0), num * batteryNum);
            materialResult.setGroupNum(num);
            r.add(materialResult);
        }
    }
}

